#if defined _td_main_included
	#endinput
#endif

#define _td_main_included

#define NO_DEALER -1
#define MAX_DEALER_NAME_LEN 32
#define TASK_FINISH_BUYING 999999

#include <amxmodx>
#include <amxmisc>
#include <cstrike>


enum _:DealerInfo {
	Di_Name[MAX_DEALER_NAME_LEN],
	DealerRefreshAction:Di_RefreshItems,
	Di_RefreshItemsMinCount,
	Di_RefreshItemsDropOld,
	Di_Registered
}

enum _:ItemInfo {
	Ii_MinCount,
	Ii_MaxCount,
	Ii_MinChance,
	Ii_MaxChance,
	Ii_MinCost,
	Ii_MaxCost
}

enum _:DealerItemInfo{
	Dii_Count,
	Dii_Cost
}

new gCvarAutoBuyCostType;
new gCvarAutoBuyCostVal;
new gCvarBuyTimeType;
new gCvarBuyTimeVal;
new gCvarBuyPlaceType;
new gCvarMpBuyTime;
new gCvarBuyPlaceDistance;

// This stores informations on dealer
new gDealerDef[MAX_DEALERS][DealerInfo];

// This stores information about what items dealer can have, price and count
new gDealersItemsDefs[MAX_DEALERS][Items][ItemInfo];

// This stores informations about real items dealer has
new gDealersItems[MAX_DEALERS][Items][DealerItemInfo];

// This stores information on whitch dealer player selects from menu
new playerDealer[MAX_PLAYERS];

// This stores information on whitch item player selects from menu
new playerItem[MAX_PLAYERS];

// This stores information on whitch dealer is available to player
new playerAvailableDealers[MAX_PLAYERS][MAX_DEALERS];

// This stores information on whitch item is available to player
new playerAvailableItems[MAX_PLAYERS][Items];

/*	This registeres cvars auto_buy_cost_type and auto_buy_cost_val
*/
new bool:gBuyTimeEnd;
new Float:gPlayerSpawnPosition[MAX_PLAYERS][3];

stock plugin_init_main(){
	register_def_cvars();
	register_logevent("RoundStart_main",2,"1=Round_Start");
}

public RoundStart_main() {
	if(task_exists(TASK_FINISH_BUYING))
		remove_task(TASK_FINISH_BUYING);
	gBuyTimeEnd = false;
	new type= get_pcvar_num(gCvarBuyTimeType);
	new delay = 0;
	switch(type){
		case 1: delay = floatround(get_pcvar_float(gCvarMpBuyTime) * 60.0);
		case 2: delay = get_pcvar_num(gCvarBuyTimeVal);
		default: return;
	}
	set_task(float(delay), "td_finish_buying", TASK_FINISH_BUYING);
}

public td_finish_buying(taskId){
	gBuyTimeEnd = true;
	
}

stock bool:td_is_user_in_buy_zone(id){
	new type = get_pcvar_num(gCvarBuyPlaceType);
	switch(type){
		case 0: return true;
		case 1: return bool:cs_get_user_buyzone(id);
		case 2: {
			new Float:pOrigin[3];
			get_user_origin(id,pOrigin);
			if(get_distance(pOrigin, gPlayerSpawnPosition[id]) <= get_pcvar_num(gCvarBuyPlaceDistance))
				return true;
		}
	}
	return false;
}

stock register_def_cvars(){
	
	new cvar[64];
	formatex(cvar, charsmax(cvar), "%s_auto_buy_cost_type", prefix)
	gCvarAutoBuyCostType = register_cvar(cvar, CVAR_AUTO_BUY_COST_TYPE_DEF);
	
	cvar[0] = '^0';
	formatex(cvar, charsmax(cvar), "%s_auto_buy_cost_val", prefix)
	gCvarAutoBuyCostVal = register_cvar(cvar, CVAR_AUTO_BUY_COST_VAL_DEF);
	
	cvar[0] = '^0';
	formatex(cvar, charsmax(cvar), "%s_buy_time_type", prefix)
	gCvarBuyTimeType = register_cvar(cvar, CVAR_BUY_TIME_TYPE_DEF);
	
	cvar[0] = '^0';
	formatex(cvar, charsmax(cvar), "%s_buy_time_val", prefix)
	gCvarBuyTimeVal = register_cvar(cvar, CVAR_BUY_TIME_VAL_DEF);
	
	cvar[0] = '^0';
	formatex(cvar, charsmax(cvar), "%s_buy_place_type", prefix)
	gCvarBuyPlaceType = register_cvar(cvar, CVAR_BUY_PLACE_TYPE_DEF);
	
	cvar[0] = '^0';
	formatex(cvar, charsmax(cvar), "%s_buy_place_distance", prefix)
	gCvarBuyPlaceDistance = register_cvar(cvar, CVAR_BUY_PLACE_DISTANCE_DEF);
	
	gCvarMpBuyTime = get_cvar_pointer("mp_buytime");
}

stock client_connect_main(id){
	td_set_player_dealers(id, true);
	td_set_player_items(id, true);
}

stock td_get_item_name(id, item, name[], len){
	#if defined ML_ITEM_NAMES
	formatex(name, len, "%L",  id, ItemNames[item]);
	#else
	formatex(name, len, "%s", ItemNames[item]);
	#endif
}

stock td_get_string(id, StringsEnum:item, string[], len){
	#if defined ML_STRINGS
	formatex(string, len, "%L",id,  Strings[item]);
	#else
	formatex(string, len, "%s", Strings[item]);
	#endif
}
